import rarfile
import concurrent.futures
import itertools
import string
import time

# 示例用法
file_path = "C:/Users/zy820/Desktop/RA.rar"  # RAR文件路径  123qwer.  123qwr
extract_to = "./filedir/"  # 解压到的目标文件夹路径


def extract_rar_with_password(password):
    with rarfile.RarFile(file_path) as rf:
        try:
            rf.extractall(path=extract_to, pwd=password)
            print(f"解压成功！解压密码：{password}")
        except Exception as e:
            return


def generate_password_dictionary(length=8):
    # 大写字母、小写字母、数字、特殊字符
    uppercase_letters = string.ascii_uppercase
    lowercase_letters = string.ascii_lowercase
    digits = string.digits
    special_characters = string.punctuation
    # 将所有字符组合起来
    #all_characters = uppercase_letters + lowercase_letters + digits + special_characters



    all_characters = lowercase_letters + digits
    # 使用itertools.product()生成密码组合
    count = 0
    start_sec = time.time()

    with concurrent.futures.ThreadPoolExecutor(max_workers=44) as executor:

        for i in itertools.product(all_characters, repeat=length):
            count += 1
            if count % 20000 == 0:
                end_sec = time.time()
                print(count / (end_sec - start_sec))
                print(count, i)
            executor.submit(extract_rar_with_password, i)


generate_password_dictionary(6)
